package proj2501.simple_module;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import proj2501.net_logo.NLButton;
import proj2501.net_logo.NLTurtle;
import proj2501.net_logo.NetLogo;

class P007NetLogo extends JPanel {
	private NetLogo nl;
	public P007NetLogo() {
		System.out.println("nl");
		nl = new NetLogo();
		nl.setRefreshMode(NetLogo.REFRESH_MODE_ON_TICK);
		setLayout(new BorderLayout());
		add(nl.getNewView());
		
//		setup();
		nl.registerCommand("setup", new Runnable() {
			@Override
			public void run() {
				setup();
			}
		});
		NLButton b1 = nl.getNewButton("setup", false, false);
		b1.doClick();
		
		nl.registerCommand("go", new Runnable() {
			@Override
			public void run() {
				go();
			}
		});
		NLButton goButton = nl.getNewButton("go", true, true);
		goButton.doClick();
	}
	private void setup() {
		nl.clearAll();
		for (int i = 0; i < 20; i++) {
			NLTurtle t = nl.createTurtle();
			double rx = nl.randomXcor();
			double ry = nl.randomYcor();
			t.setxy(rx, ry);
		}
	}
	
	private void go() {
		moveTurtles();
		nl.tick();
	}
	
	private void moveTurtles() {
		for (NLTurtle turtle : nl.turtles()) {
			turtle.right(nl.random(20) - 10);
			turtle.jump(0.5);
//			turtle.jump(0.1);
		}
	}
}
